package com.snail.readyvr.ui;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;

import com.snail.readyvr.R;


/**
 * Created by wangyunpeng on 2016/3/11.
 * 自定义一个带删除按钮的输入框
 */
public class ShowPassWordEditText extends EditText implements TextWatcher {
    private boolean isHidden = true;
    /**
     * 不显示密码按钮的引用
     */
    private Drawable mNoShowDrawable;
    /**
     * 显示密码按钮的引用
     */
    private Drawable mShowDrawable;

    /**
     * 控件是否有焦点
     */
    private boolean hasFoucs;

    public ShowPassWordEditText(Context context) {
        this(context, null);
    }

    private TextWatcher mTextWatcher;

    public ShowPassWordEditText(Context context, AttributeSet attrs) {
        //这里构造方法也很重要，不加这个很多属性不能再XML里面定义
        this(context, attrs, android.R.attr.editTextStyle);
    }

    public ShowPassWordEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }


    private void init() {
        //获取EditText的DrawableRight,假如没有设置我们就使用默认的图片
       /* mClearDrawable = getCompoundDrawables()[2];
        if (mClearDrawable == null) {*/
//        	throw new NullPointerException("You can add drawableRight attribute in XML");
        mNoShowDrawable = getResources().getDrawable(R.drawable.unvisible);
       // }
        mShowDrawable = getResources().getDrawable(R.drawable.visible);

        mNoShowDrawable.setBounds(0, 0, mNoShowDrawable.getIntrinsicWidth(), mNoShowDrawable.getIntrinsicHeight());
        mShowDrawable.setBounds(0, 0, mShowDrawable.getIntrinsicWidth(), mShowDrawable.getIntrinsicHeight());
        //默认设置隐藏图标
        setIconVisible();
        setTransformationMethod(PasswordTransformationMethod.getInstance());
        //设置输入框里面内容发生改变的监听
        addTextChangedListener(this);
    }


    /**
     * 因为我们不能直接给EditText设置点击事件，所以我们用记住我们按下的位置来模拟点击事件
     * 当我们按下的位置 在  EditText的宽度 - 图标到控件右边的间距 - 图标的宽度  和
     * EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标，竖直方向就没有考虑
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (getCompoundDrawables()[2] != null) {

                boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())
                        && (event.getX() < ((getWidth() - getPaddingRight())));

                if (touchable) {
                    if (isHidden) {
                        //设置EditText文本为可见的
                        setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    } else {
                        //设置EditText文本为隐藏的
                        setTransformationMethod(PasswordTransformationMethod.getInstance());
                    }
                    isHidden = !isHidden;
                    setIconVisible();
                    //切换后将EditText光标置于末尾
                    CharSequence charSequence = getText();
                    if (charSequence instanceof Spannable) {
                        Spannable spanText = (Spannable) charSequence;
                        Selection.setSelection(spanText, charSequence.length());
                    }
                }
            }
        }

        return super.onTouchEvent(event);
    }


    /**
     * 设置清除图标的显示与隐藏，调用setCompoundDrawables为EditText绘制上去
     *
     */
    protected void setIconVisible() {
        Drawable right = isHidden ? mNoShowDrawable : mShowDrawable;
        setCompoundDrawables(getCompoundDrawables()[0],
                getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
    }


    /**
     * 当输入框里面内容发生变化的时候回调的方法
     */
    @Override
    public void onTextChanged(CharSequence s, int start, int count,
                              int after) {

        if(mTextWatcher != null)
            mTextWatcher.onTextChanged(s,start,count,after);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
        if(mTextWatcher != null)
            mTextWatcher.beforeTextChanged(s,start,count,after);
    }

    @Override
    public void afterTextChanged(Editable s) {
        if(mTextWatcher != null)
            mTextWatcher.afterTextChanged(s);
    }

    public void setTextWatcher(TextWatcher textWatcher) {
        mTextWatcher = textWatcher;
    }
}
